<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \interop\Container\ContainerInterface;

class HomeController 
{
    protected $app;

    public function __construct(ContainerInterface $ci)
    {
        $this->app = $ci;
    }

    public function home(Request $request, Response $response, $args)
    {	
    	return $this->app->view->render($response, 'index.html',['model'=>$args['model_id']]);
    }

    public function home1(Request $request, Response $response, $args)
    {   
        return $this->app->view->render($response, 'index1.html',['model'=>$args['model_id']]);
    }
    
    public function getcate(Request $request, Response $response, $args){
        // $rs = $this->app->db->table('cate')->get();
        // foreach($rs as $v){
        //     if($v->lev == 1){
        //         $arr[0][$v->cid]['cid'] = $v->cid;
        //         $arr[0][$v->cid]['pid'] = $v->pid;
        //         $arr[0][$v->cid]['name'] = $v->name;
        //     }     
        // }
        // foreach($arr[0] as $m){
        //     foreach($rs as $v){
        //         if($v->lev == 2 && $m->cid == $v->pid){
        //             $arr[1][$m->cid]['cid'] = $v->cid;
        //             $arr[1][$m->cid]['pid'] = $v->pid;
        //             $arr[1][$m->cid]['name'] = $v->name;
        //         }
        //     }
        // }
        

        

    	$rs = $this->app->db->table('cate')->where('lev',1)->get();
        if(!$rs){
            print_r('error');exit;
        }
	    foreach($rs as $k=>$v){
    		$arr[$v->cid]['cid'] = $v->cid;
    		$arr[$v->cid]['pid'] = $v->pid;
    		$arr[$v->cid]['name'] = $v->name;
    	}
    	return $response->withJson($arr);
    }

    public function result(Request $request, Response $response, $args){

    	$cid = $_POST['cid'];
    	$a = $this->app->db->table('categroy')->where('pid',$cid)->get();
    	if(!$a){
            
            $brand = $this->app->db->table('brand')->get();

            foreach($brand as $v){
                $arr[$v->brand_id]['name'] = $v->brand_name;
            }
            return $response->withJson($arr);
    		// return false;
    	}else{
            foreach($a as $k=>$v){
                $arr[$v->cid]['cid'] = $v->cid;
                $arr[$v->cid]['pid'] = $v->pid;
                $arr[$v->cid]['name'] = $v->name;
            }
            // print_r($arr);exit;

            // $b = $this->sortOut($cid,$arr);
            return $response->withJson($arr);
        }
    	
    }
}